package cn.smbms.contorller.user;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.pojo.User;
import cn.smbms.service.role.RoleService;
import cn.smbms.service.user.UserService;
import cn.smbms.service.user.UserServiceImpl;
import cn.smbms.tools.ACT;
import cn.smbms.tools.Constants;

@Controller
@RequestMapping("/open")
public class LoginController {
	private static Logger logger=Logger.getLogger(LoginController.class);
	
	
	/*
	 * 用户业务实现类属性对象
	 */
	@Resource(name="userServiceImpl")
	private UserServiceImpl userService;
	
	
	
	
	
	/*
	 * 进入登陆界面方法
	 */
	@RequestMapping(value="/login")
	public String login(HttpSession session
			,HttpServletRequest request,
			HttpServletResponse response){
		logger.debug("==================login");
		return "login";
	}
	
	/*
	 * 进入验证登录信息方法
	 */
	@RequestMapping(value="/entrance")
	public String entrance(HttpSession session
			,HttpServletRequest request,
			HttpServletResponse response
			,@RequestParam(value="userCode",required=false)String userCode,
			@RequestParam(value="userPassword",required=false)String userPassword)
			throws ServletException, IOException {
		
		
		logger.debug(" ============login " );
		//调用service方法，进行用户匹配
		User user = userService.login(userCode,userPassword);
		if(null != user){//登录成功
			//放入session
			session.setAttribute(Constants.USER_SESSION, user);
			session.setMaxInactiveInterval(20*60);
			//页面跳转（frame.jsp）
			return "frame";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			/*request.setAttribute("error", "用户名或密码不正确");*/
			throw new RuntimeException("用户名或密码不正确");
			/*return "login";*/
		}
	}
	
	/*本类方法异常监控*/
	/*@ExceptionHandler(value={RuntimeException.class})
	public String handerExcepion(RuntimeException ex,HttpServletRequest request, 
			HttpServletResponse response){
		request.setAttribute("error", ex);
		return "error";
	}*/
	
}

